<script setup lang="ts">
import { delDict } from '@/api/dict'
import type { DictTable } from '@/types/dict'
import { ElMessage } from 'element-plus'

const props = defineProps<{
  tableHeadDictMap: Map<string, string>
  rows: DictTable[]
  refresh: () => void
}>()
const emit = defineEmits<{
  (event: 'cDlg'): void
}>()

function cancel() {
  emit('cDlg')
}

function del() {
  let ids: number[] = []
  props.rows.forEach((d) => {
    ids.push(d.dId!)
  })
  delDict(ids)
    .then((d) => {
      props.refresh()
      ElMessage.success(d.msg)
      emit('cDlg')
    })
    .catch((d) => {
      ElMessage.error(d.msg)
    })
}
</script>

<template>
  <el-table :data="rows">
    <el-table-column prop="dId" align="center">
      <template #header>
        {{ tableHeadDictMap?.get('dId') || 'dId' }}
      </template>
    </el-table-column>
    <el-table-column prop="name" align="center">
      <template #header>
        {{ tableHeadDictMap?.get('name') || 'name' }}
      </template>
    </el-table-column>
    <el-table-column prop="type" align="center">
      <template #header>
        {{ tableHeadDictMap?.get('type') || 'type' }}
      </template>
      <template #default="{ row }">
        <span class="highlight-info">{{ row['type'] }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="path" align="center">
      <template #header>
        {{ tableHeadDictMap?.get('path') || 'path' }}
      </template>
      <template #default="{ row }">
        <span class="highlight-info">{{ row['path'] }}</span>
      </template>
    </el-table-column>
    <el-table-column prop="description" align="center">
      <template #header>
        {{ tableHeadDictMap?.get('description') || 'description' }}
      </template>
    </el-table-column>
  </el-table>
  <div class="buttons">
    <el-button @click="cancel">取消</el-button>
    <el-button type="primary" @click="del"> 确定 </el-button>
  </div>
</template>

<style scoped lang="less">
.buttons {
  display: flex;
  margin-top: 30px;
  justify-content: flex-end;
}
</style>
